import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

void main() {
  runApp(const MaterialApp(home: HomePage()));
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  bool flag = false;
  bool flag2 = false;
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: const Text('你好flutter1'),
            ),
            body: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // TODO AnimatedPadding
                Row(
                  children: [
                    AnimatedPadding(
                      padding: EdgeInsets.fromLTRB(!flag2 ? 0 : 100, 0, 0, 0),
                      duration: const Duration(seconds: 1),
                      child: Container(
                        height: 100,
                        width: 100,
                        color: Colors.red,
                      ),
                    ),
                    IconButton(
                      onPressed: () {
                        setState(() {
                          flag2 = !flag2;
                        });
                      },
                      icon: const Icon(Icons.change_circle),
                    )
                  ],
                ),
                // TODO AnimatedOpacity
                AnimatedOpacity(
                  opacity: !flag2 ? 0.2 : 1,
                  duration: const Duration(seconds: 1),
                  child: Container(
                    height: 100,
                    width: 100,
                    color: Colors.teal,
                  ),
                ),
                // TODO AnimatedDefaultTextStyle
                SizedBox(
                  height: 150,
                  child: AnimatedDefaultTextStyle(
                    style: TextStyle(
                      fontSize: flag2 ? 20 : 40,
                      color: flag2 ? Colors.black : Colors.orange,
                    ),
                    duration: const Duration(seconds: 1),
                    child: const Text('你好a'),
                  ),
                ),
                // TODO AnimatedSwitcher 组件切换动画
                SizedBox(
                  width: 200,
                  height: 200,
                  child: AnimatedSwitcher(
                    duration: const Duration(seconds: 1),
                    child: !flag2
                        ? const CircularProgressIndicator()
                        : Image.network(
                            'https://www.itying.com/images/flutter/1.png',
                            fit: BoxFit.cover,
                          ),
                  ),
                ),
                // TODO AnimatedSwitcher  +  transitionBuilder 组件切换动画
                Container(
                  width: 200,
                  height: 100,
                  color: Colors.blue,
                  child: AnimatedSwitcher(
                    duration: const Duration(seconds: 1),
                    // 改变动画效果，返回动画组件
                    transitionBuilder: (child, animation) {
                      return ScaleTransition(
                        scale: animation,
                        child: child,
                      );
                    },
                    // 加上key，每次改变就不是同一个组件了
                    child: Text(
                      flag2 ? '你好吗？' : '我很好！',
                      key: UniqueKey(),
                      style: const TextStyle(fontSize: 30),
                    ),
                  ),
                ),
              ],
            ),
            floatingActionButton: FloatingActionButton(
              onPressed: () {
                setState(() {
                  flag = !flag;
                });
              },
              child: const Icon(Icons.add),
            ),
          ),
        ),
        // TODO AnimatedContainer 动画Container
        Positioned(
          child: AnimatedContainer(
            // curve: Curves.bounceIn,
            duration: const Duration(milliseconds: 400),
            height: double.infinity,
            width: 200,
            transform: !flag
                ? Matrix4.translationValues(-200, 0, 0)
                : Matrix4.translationValues(0, 0, 0),
            color: Colors.orange,
          ),
        ),
        // TODO AnimatedPositioned
        AnimatedPositioned(
          right: flag2 ? 300 : 0,
          bottom: flag2 ? 0 : 500,
          width: 100,
          height: 100,
          duration: const Duration(seconds: 1),
          child: Container(
            height: 100,
            width: 100,
            color: Colors.pink,
          ),
        ),
      ],
    );
  }
}
